package cn.springcloud.book.controller;

import cn.springcloud.book.config.ConfigInfoProperties;
import com.asyf.MyStarterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@Api(value = "测试接口")
@RefreshScope//手动刷新---注意刷新需要post方法
public class TestController {

    @Value(value = "${server.port}")
    private String port;

//    @Value(value = "${test.config}")
//    private String test;

    //自定义starter测试--将resources/jar/custom-spring-boot-starter-1.0-SNAPSHOT.jar安装到本地方可测试
    @Autowired
    private MyStarterService myStarterService;

    @Autowired
    private ConfigInfoProperties configInfoProperties;

    @RequestMapping(value = "/portTest", method = RequestMethod.GET)
    public String test(HttpServletRequest request) throws Exception {
        log.info("test接口，端口号：" + port);
        log.info("myStarterService:" + myStarterService.heart());
        return "test接口，端口号：" + port + ",test:" + configInfoProperties.getConfig();
    }

    @ApiOperation(value = "加法", notes = "加法")
    @GetMapping("/add")
    public Integer add(Integer a, Integer b) {
        return a + b;
    }

    @GetMapping("/a/add")
    @ApiIgnore
    public Integer aadd(Integer a, Integer b) {
        return a + b;
    }

    @GetMapping("/sub")
    public Integer sub(Integer a, Integer b) {
        return a - b;
    }

    @GetMapping("/mul")
    public String mul(Integer a, Integer b) {
        System.out.println("进入client-a!");
        return "client-a-" + a * b;
    }

    @GetMapping("/div")
    public Integer div(Integer a, Integer b) {
        return a / b;
    }
}
